package org.jeecg.modules.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wms.entity.FinishedGoodsInventoryLog;
import org.jeecg.modules.wms.service.IFinishedGoodsInventoryLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 成品库存变更记录控制器
 */
@Api(tags = "成品库存变更记录")
@RestController
@RequestMapping("/wms/finished-goods/inventory-log")
@Slf4j
public class FinishedGoodsInventoryLogController extends JeecgController<FinishedGoodsInventoryLog, IFinishedGoodsInventoryLogService> {

    @Autowired
    private IFinishedGoodsInventoryLogService inventoryLogService;

    /**
     * 分页查询库存变更记录
     */
    @AutoLog(value = "成品库存变更记录-分页列表查询")
    @ApiOperation(value = "成品库存变更记录-分页列表查询", notes = "成品库存变更记录-分页列表查询")
    @GetMapping(value = "/records")
    public Result<IPage<FinishedGoodsInventoryLog>> getRecords(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "orderNo", required = false) String orderNo,
            @RequestParam(name = "operationType", required = false) Integer operationType,
            @RequestParam(name = "relatedDocNo", required = false) String relatedDocNo,
            @RequestParam(name = "operatorName", required = false) String operatorName,
            @RequestParam(name = "operateTimeStart", required = false) String operateTimeStart,
            @RequestParam(name = "operateTimeEnd", required = false) String operateTimeEnd,
            HttpServletRequest req) {
        
        try {
            Page<FinishedGoodsInventoryLog> page = new Page<>(pageNo, pageSize);
            LambdaQueryWrapper<FinishedGoodsInventoryLog> queryWrapper = new LambdaQueryWrapper<>();
            
            // 构建查询条件
            if (StringUtils.isNotBlank(orderNo)) {
                queryWrapper.like(FinishedGoodsInventoryLog::getOrderNo, orderNo);
            }
            if (operationType != null) {
                queryWrapper.eq(FinishedGoodsInventoryLog::getOperationType, operationType);
            }
            if (StringUtils.isNotBlank(relatedDocNo)) {
                queryWrapper.like(FinishedGoodsInventoryLog::getRelatedDocNo, relatedDocNo);
            }
            if (StringUtils.isNotBlank(operatorName)) {
                queryWrapper.like(FinishedGoodsInventoryLog::getOperatorName, operatorName);
            }
            if (StringUtils.isNotBlank(operateTimeStart)) {
                queryWrapper.ge(FinishedGoodsInventoryLog::getCreateTime, operateTimeStart);
            }
            if (StringUtils.isNotBlank(operateTimeEnd)) {
                queryWrapper.le(FinishedGoodsInventoryLog::getCreateTime, operateTimeEnd);
            }
            
            // 按创建时间倒序排列
            queryWrapper.orderByDesc(FinishedGoodsInventoryLog::getCreateTime);
            
            IPage<FinishedGoodsInventoryLog> pageList = inventoryLogService.page(page, queryWrapper);
            
            return Result.OK(pageList);
            
        } catch (Exception e) {
            log.error("查询成品库存变更记录失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询记录详情
     */
    @AutoLog(value = "成品库存变更记录-通过id查询")
    @ApiOperation(value = "成品库存变更记录-通过id查询", notes = "成品库存变更记录-通过id查询")
    @GetMapping(value = "/detail/{id}")
    public Result<FinishedGoodsInventoryLog> getDetail(@PathVariable("id") String id) {
        try {
            FinishedGoodsInventoryLog record = inventoryLogService.getById(id);
            if (record == null) {
                return Result.error("记录不存在");
            }
            return Result.OK(record);
        } catch (Exception e) {
            log.error("查询记录详情失败，ID: {}", id, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据库存ID查询变更记录
     */
    @AutoLog(value = "成品库存变更记录-根据库存ID查询")
    @ApiOperation(value = "成品库存变更记录-根据库存ID查询", notes = "成品库存变更记录-根据库存ID查询")
    @GetMapping(value = "/by-inventory/{inventoryId}")
    public Result<IPage<FinishedGoodsInventoryLog>> getByInventoryId(
            @PathVariable("inventoryId") String inventoryId,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        
        try {
            Page<FinishedGoodsInventoryLog> page = new Page<>(pageNo, pageSize);
            LambdaQueryWrapper<FinishedGoodsInventoryLog> queryWrapper = new LambdaQueryWrapper<>();
            
            queryWrapper.eq(FinishedGoodsInventoryLog::getInventoryId, inventoryId)
                       .orderByDesc(FinishedGoodsInventoryLog::getCreateTime);
            
            IPage<FinishedGoodsInventoryLog> pageList = inventoryLogService.page(page, queryWrapper);
            
            return Result.OK(pageList);
            
        } catch (Exception e) {
            log.error("根据库存ID查询变更记录失败，库存ID: {}", inventoryId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据订单号查询变更记录
     */
    @AutoLog(value = "成品库存变更记录-根据订单号查询")
    @ApiOperation(value = "成品库存变更记录-根据订单号查询", notes = "成品库存变更记录-根据订单号查询")
    @GetMapping(value = "/by-order/{orderNo}")
    public Result<IPage<FinishedGoodsInventoryLog>> getByOrderNo(
            @PathVariable("orderNo") String orderNo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        
        try {
            Page<FinishedGoodsInventoryLog> page = new Page<>(pageNo, pageSize);
            LambdaQueryWrapper<FinishedGoodsInventoryLog> queryWrapper = new LambdaQueryWrapper<>();
            
            queryWrapper.eq(FinishedGoodsInventoryLog::getOrderNo, orderNo)
                       .orderByDesc(FinishedGoodsInventoryLog::getCreateTime);
            
            IPage<FinishedGoodsInventoryLog> pageList = inventoryLogService.page(page, queryWrapper);
            
            return Result.OK(pageList);
            
        } catch (Exception e) {
            log.error("根据订单号查询变更记录失败，订单号: {}", orderNo, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }
}
